package com.lv.phuotbacnam.ui.fragment;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.log.Logger;
import com.lv.phuotbacnam.bl.queries.QueriesConstant;
import com.lv.phuotbacnam.bl.utils.IntentHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.parse.ParseObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class FullImage extends Activity implements SwipeInterface
{
	
	ParseObject object_diadiem =  (ParseObject) IntentHelper.getObjectForKey("diadiem");
	JSONArray list_photo = object_diadiem.getJSONArray(QueriesConstant.TableDiaDiem.COLUMN_list_photo);
	DisplayImageOptions options;
	ImageLoader imageLoader;
	int positionImage;
	Button btn_close;
	ImageView fullImage;
	LinearLayout lowestLayout;
	//ArrayList<String> arrUrlImage = new ArrayList<String>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_fullscreen);
		btn_close = (Button)findViewById(R.id.btnClose);
		fullImage = (ImageView)findViewById(R.id.image_full);
		Intent callerIntent=getIntent();
		Bundle packageFromCaller=
				 callerIntent.getBundleExtra("MyPackage");
		
		positionImage = packageFromCaller.getInt("position");
		//arrUrlImage = packageFromCaller.getStringArrayList("arrayUrlImage");
		
		//positionImage = list_photo.getJSONObject(0).getString("url");
		
		imageLoader = ImageLoader.getInstance();
		
		//cache image
		options = new DisplayImageOptions.Builder().cacheInMemory(true)
								.cacheOnDisc(true).resetViewBeforeLoading(true).build();
        //Initialize ImageLoader with configuration. Do it once.
        imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseContext()));
		try {
			imageLoader.displayImage(list_photo.getJSONObject(0).getString("url"),fullImage,options);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//Logger.logMessage("position : " + packageFromCaller.getInt("posotion"));
		
		// close image full screen ve lai chitietdiadiem
		btn_close.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				FullImage.this.finish();
			}
		});
		
		//slide
		//ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this,false,false);
		lowestLayout = (LinearLayout)findViewById(R.id.layout_fullimage);
		lowestLayout.setOnTouchListener(new ActivitySwipeDetector(this, FullImage.this));
		
			
		
		//Logger.logMessage("ec ec ec " + activitySwipeDetector.isLeftToright());
		/*if(activitySwipeDetector.isLeftToright() == true)
		{
			activitySwipeDetector.setLeftToright(false);
		}
		else if (activitySwipeDetector.isLeftToright() == false)
		{
			Logger.logMessage("ec ec ec");
		}
		if(activitySwipeDetector.isRightToleft())
		{
			Logger.logMessage("sao ko dc ta");
			imageLoader.displayImage(arrUrlImage.get(positionImage),fullImage);
			activitySwipeDetector.setRightToleft(false);
		}
		else Logger.logMessage("sao ko dc ta 2");
		*/
	}
	@Override
	public void onLeftToRight(View v) {
		// TODO Auto-generated method stub
		 Log.e("TAG", "L to R");
		 if(positionImage>0)
		 {
			 positionImage--;
			 try {
				imageLoader.displayImage(list_photo.getJSONObject(positionImage).getString("url"),fullImage,options);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
		
	}
	@Override
	public void onRightToLeft(View v) {
		// TODO Auto-generated method stub
		 Log.e("TAG", "R to L");
		 if(positionImage < (list_photo.length() -1))
		 {
			 positionImage++;
			 try {
				imageLoader.displayImage(list_photo.getJSONObject(positionImage).getString("url"),fullImage,options);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }		 
		
	}

}
